package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.core.BottomPopupView;
import com.sgb.member.R;
import com.sgb.member.databinding.MemberChooseLeftItemBinding;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.GridItemDecoration;
import com.swgk.core.view.selection.XChooseChildAdapter;
import com.swgk.core.view.selection.XChooseEntity;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ViewConstructor")
public class MultiMemberChoosePopup extends BottomPopupView {

    private final String titles;
    private MaxRecyclerView recyclerView;
    // 缓存数据集
    private final SparseArray<XChooseEntity> mData = new SparseArray<>();
    // 勾选缓存
    private final SparseIntArray mFilters = new SparseIntArray();
    private BaseRecycleAdapter<XChooseEntity> mMainAdapter;

    public MultiMemberChoosePopup(@NonNull Context context, int height, List<CommonPopEntity> list, String title, OnConfirmListener confirmListener) {
        super(context);
        setConfirmListener(confirmListener);
        titles = title;
        mData.put(0, new XChooseEntity(0, titles, list));
        LayoutParams layoutParams = (LayoutParams) bottomPopupContainer.getLayoutParams();
        layoutParams.topMargin = height;
        bottomPopupContainer.setLayoutParams(layoutParams);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.xchoose_left_dialog;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView resetView = findViewById(R.id.reset_view);
        resetView.setText("取消");
        resetView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick()) {
                    dismiss();
                }
            }
        });
        findViewById(R.id.ok_view).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick()) {
                    CommonPopEntity mCurBean = null;
                    List<CommonPopEntity> objects = new ArrayList<>();
                    for (int i = 0; i < mFilters.size(); i++) {
                        int key = mFilters.keyAt(i);
                        int value = mFilters.get(key);
                        if (value > 0) {
                            mCurBean = mData.get(key).getList().get(value);
                            objects.add(mCurBean);
                        }
                    }
                    if (mCurBean == null) {
                        MToast.showToast(getContext(), "请选择" + titles);
                        return;
                    }
                    if (TextUtils.equals(mCurBean.getTypeName(), "全部") || TextUtils.equals(mCurBean.getTypeName(), "不限") || TextUtils.equals(mCurBean.getTypeName(), "全国")) {
                        MToast.showToast(getContext(), "请选择" + titles);
                        return;
                    }
                    dismiss();
                    if (confirmListener != null) {
                        confirmListener.onConfirm(mCurBean);
                    }
                    onResult(mCurBean, objects.toArray());
                }
            }
        });

        recyclerView = findViewById(R.id.type_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        /*List<ChooseEntity> list = new ArrayList<>();
        ChooseEntity entity0 = new ChooseEntity(0, "一级分类名称", new ArrayList<>());
        ChooseEntity entity1 = new ChooseEntity(0, "二级分类名称", new ArrayList<>());
        ChooseEntity entity2 = new ChooseEntity(0, "三级分类名称", new ArrayList<>());
        ChooseEntity entity3 = new ChooseEntity(0, "四级分类名称", new ArrayList<>());
        ChooseEntity entity4 = new ChooseEntity(0, "五级分类名称", new ArrayList<>());
        list.add(entity0);
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        list.add(entity4);
        for (int i = 0; i < list.size(); i++) {
            int size = i * 4 + 4;
            for (int j = 0; j < size - 1; j++) {
                CommonPopEntity obj = new CommonPopEntity();
                if (j == 0) {
                    obj.setNo("");
                    obj.setTypeName("全部");
                } else {
                    String no = String.valueOf((i + 1)) + String.valueOf(j);
                    obj.setNo(no);
                    obj.setTypeName(list.get(i).getTypeName().substring(0, 1) + "级分类" + j);
                }
                list.get(i).getList().add(obj);
            }
        }*/
        initMainAdapter();
    }

    public void initMainAdapter() {
        List<XChooseEntity> entities = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            XChooseEntity datum = mData.get(key);
            if (datum.getList() == null || datum.getList().size() == 0) {
                continue;
            }
            entities.add(datum);
        }
        mMainAdapter = new BaseRecycleAdapter<XChooseEntity>(getContext(), R.layout.member_choose_left_item, entities) {
            @Override
            public void convert(ItemViewHolder holder, XChooseEntity o) {
                MemberChooseLeftItemBinding testBinding = (MemberChooseLeftItemBinding) holder.getmBinding();
                testBinding.tvCategory.setText(o.getTypeName());
                testBinding.arrowLayout.setVisibility(INVISIBLE);
                updateChildRecycler(testBinding.rv, o, mFilters.get(o.getSort()), true);
            }
        };
        recyclerView.setAdapter(mMainAdapter);
    }

    public void updateChildRecycler(RecyclerView recyclerView, XChooseEntity parentEntity, int defItem, boolean isExpand) {
        XChooseChildAdapter mAdapter = new XChooseChildAdapter(getContext(), parentEntity.getList(), R.layout.member_choose_left_child_item, isExpand);
        mAdapter.setItemSelect(true);
        if (recyclerView.getItemDecorationCount() > 0) {
            recyclerView.removeItemDecorationAt(0);
        }
        recyclerView.addItemDecoration(new GridItemDecoration(10));
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setDefItem(defItem);
        mAdapter.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                mFilters.put(parentEntity.getSort(), position);
                entity.setId(parentEntity.getSort() + ""); // 记录是几级筛选
                for (int i = 0; i < mData.size(); i++) {
                    int key = mData.keyAt(i);
                    XChooseEntity datum = mData.get(key);
                    if (datum.getList() == null || datum.getList().size() == 0) {
                        continue;
                    }
                    if (key > parentEntity.getSort()) {
                        mData.get(key).setList(new ArrayList<>());
                        mFilters.put(key, 0);
                    }
                }
                if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                    if (!entity.getChildren().get(0).getTypeName().equals("全部")) {
                        entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                    }
                    int sort = parentEntity.getSort() + 1;
                    mData.put(sort, new XChooseEntity(sort, titles + "子类", entity.getChildren()));
                } else {
                    for (int i = 0; i < mData.size(); i++) {
                        int key = mData.keyAt(i);
                        XChooseEntity datum = mData.get(key);
                        if (datum.getList() == null || datum.getList().size() == 0) {
                            continue;
                        }
                        if (key > parentEntity.getSort()) {
                            mData.get(key).setList(new ArrayList<>());
                        }
                    }
                }
                List<XChooseEntity> entities = new ArrayList<>();
                for (int i = 0; i < mData.size(); i++) {
                    int key = mData.keyAt(i);
                    XChooseEntity datum = mData.get(key);
                    if (datum.getList() == null || datum.getList().size() == 0) {
                        mFilters.put(datum.getSort(), 0);
                        continue;
                    }
                    entities.add(datum);
                }
                mMainAdapter.setDatas(entities);
            }
        });
    }

    public void onResult(CommonPopEntity t, Object... objects) {

    }

    private OnConfirmListener confirmListener;

    public void setConfirmListener(OnConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public interface OnConfirmListener {
        void onConfirm(CommonPopEntity entity);
    }
}
